/*
 * ContactImpl.java
 *
 * Created on August 27, 2007, 11:36 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package uk.ac.ebi.bioinvindex.model.impl;

import uk.ac.ebi.bioinvindex.model.impl.term.ContactRoleImpl;

import javax.persistence.*;

/**
 * @author brandizi
 */
@Entity
@Table(name = "contact")
public class ContactImpl extends AnnotatableImpl
{

    private StudyImpl study;
    private InvestigationImpl investigation;
    private String firstName;
    private String lastName;
    private String midInitials;
    private String email;
    private String phone;
    private String fax;
    private String affiliation;
    private String url;
    private ContactRoleImpl role;

    protected ContactImpl() {
    }


    public ContactImpl(String firstName, String midInitials, String lastName, String email) {
        // TODO: Ignore nulls?
        this.setFirstName(firstName);
        this.setMidInitials(midInitials);
        this.setLastName(lastName);
        this.setEmail(email);
    }


    @ManyToOne(targetEntity = uk.ac.ebi.bioinvindex.model.impl.StudyImpl.class)
    @JoinColumn(name = "study_id", nullable = true)
    public StudyImpl getStudy() {
        return study;
    }

    public void setStudy(StudyImpl study) {
        this.study = study;
    }


    @ManyToOne(targetEntity = uk.ac.ebi.bioinvindex.model.impl.InvestigationImpl.class)
    @JoinColumn(name = "investigation_id", nullable = true)
    public InvestigationImpl getInvestigation() {
        return investigation;
    }

    public void setInvestigation (InvestigationImpl investigation) {
        this.investigation = investigation;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMidInitials() {
        return midInitials;
    }

    public void setMidInitials(String midInitials) {
        this.midInitials = midInitials;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getAffiliation() {
        return affiliation;
    }

    public void setAffiliation(String affiliation) {
        this.affiliation = affiliation;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    @ManyToOne(
			targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.ContactRoleImpl.class
		)
    @JoinColumn(name = "role_id", nullable = true)
    public ContactRoleImpl getRole() {
        return role;
    }

    public void setRole(ContactRoleImpl contactRole) {
        this.role = contactRole;
    }

		@Transient
		/** Gets Name+Middle+Surname */
		public String getFullName () {
			StringBuilder sb = new StringBuilder();
			sb.append(this.getFirstName());
			if (this.getMidInitials().length() > 0) {
				sb.append(" ");
				sb.append(this.getMidInitials());
			}
			sb.append(" ");
			sb.append(this.getLastName());

			return sb.toString();



//			String midStr = StringUtils.trimToEmpty ( getMidInitials () );
//			if ( midStr.length () != 0 ) midStr = " " + midStr;
//
//			return String.format ( "%s%s %s",
//				trim ( this.getFirstName () ), midStr, trim ( this.getLastName () ) );
		}

    public String toString() {
        ContactRoleImpl role = this.getRole();
        String roleS = "";
        if (role != null) roleS = String.format ( "{ '%s' ( '%s' ) }", role.getAcc(), role.getName() );

        return String.format(
					"'%s' ('%s') '%s' <%s>\n  Role: %s\n  Phone: '%s', Fax: '%s'\n  Affiliation: '%s', URL: <%s>",
          getFirstName(), getMidInitials(), getLastName(), getEmail(), roleS, getPhone(), getFax(), getAffiliation(), getUrl()
        );
    }



}
